int ledMatriex[] = { 4, 7 };           // led
int buttonMatriex[] = { 8, 12, 13 };   // button,13 is reset button
int buttonValMatriex[] = { 0, 0, 0 };  // button Val
int buzzer = 2;                        // buzzer

void setup() {
  for (int i = 0; i < 2; i++) {
    pinMode(ledMatriex[i], OUTPUT);  // led
    digitalWrite(ledMatriex[i], LOW);
  }
  for (int i = 0; i < 3; i++) {
    pinMode(buttonMatriex[i], INPUT);  // button
  }
  pinMode(buzzer, OUTPUT);  // buzzer
}

void loop() {
  for (int i = 0; i < 3; i++) {
    buttonValMatriex[i] = digitalRead(buttonMatriex[i]);  // read val
    if (buttonValMatriex[i] == LOW) {
      if (i < 2)
        digitalWrite(ledMatriex[i], LOW);
    } else {
      // 2 pressed
      if (i == 2) {
        digitalWrite(ledMatriex[0], LOW);  // led off
        digitalWrite(ledMatriex[1], LOW);  // led off
        // buzzer off
        digitalWrite(buzzer, LOW);
        break;
      }
      // 2 not pressed，go to a loop
      while (digitalRead(buttonMatriex[2]) == LOW) {
        digitalWrite(ledMatriex[i], HIGH);  // led on
        // buzzer on
        digitalWrite(buzzer, HIGH);
        tone(buzzer, 100000, 0.1);
      }
      digitalWrite(ledMatriex[0], LOW);  // led off
      digitalWrite(ledMatriex[1], LOW);  // led off
      // buzzer on
      digitalWrite(buzzer, LOW);
      noTone(buzzer);
      break;
    }
  }
}
